package com.cyloer.smartThread.Singleton;

import java.util.Random;

/**
 * @Description:
 * @author: xiangliu5
 * @date: 2022/1/26 11:51
 */
public class SingletonInner {

    private String name;
    private Long time;

    public SingletonInner(String name, Long time) {
        this.name = name;
        this.time = time;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getTime() {
        return time;
    }

    public void setTime(Long time) {
        this.time = time;
    }
    private static class SingletonClass {
        private static final SingletonInner singletonInner=new SingletonInner();
    }

    /**
     * 引入外部类，进行初始化。利用多线程缺省锁，进行同步，提高性能（原理：当引用外部类时候，会延迟加载当前类初始化，优先初始化外部类，达到线程安全目的）
     * 利用了classloader的机制来保证初始化instance时只有一个线程，所以也是线程安全的，同时没有性能损耗
     * @return 返回SingletonInner
     */
    public static SingletonInner getInstance(){
        return  SingletonClass.singletonInner;
    }
    public void println() {
        System.out.println("name:"+this.name+"\n"+"time"+this.time);
    }

    private SingletonInner() {
        initProperties();
    }

    private synchronized  void initProperties() {
      this.name="lx"+System.currentTimeMillis();
      this.time=new Random().nextLong();
    }


}
